<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService } from '@/api/article.js'
import { formatTime } from '@/utils/format'
import { artDelService } from '@/api/article'

// 定义请求参数对象
const params = ref({
  pagenum: 1, // 当前页
  pagesize: 5, // 当前生效的每页条数
  cate_id: '', //分类框 id绑定
  state: '' //状态框
})

const articleList = ref([]) // 文章列表
const total = ref(0)
const loading = ref(false)
//文章：获取文章数据列表渲染表格
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  // console.log(res)
  articleList.value = res.data.data
  total.value = res.data.total

  loading.value = false
}
getArticleList()

//处理分页
// 选择一页显示的数据条数
const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  //基于最新的当前页和 每页条数 ，渲染数据
  getArticleList()
  // console.log('size', size)  //选择一页显示的条数
}
//选择去到第几页
const onCurrentChange = (page) => {
  // console.log('page', page)
  params.value.pagenum = page

  getArticleList()
}

//搜索 搜索逻辑=>按照最新的条件，重新检索，从第一页开始展示
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
//重置
const onReset = () => {
  params.value.pagenum = 1 //重置页面
  params.value.cate_id = '' //重置分类框为空
  params.value.state = '' //重置状态为空
  getArticleList()
}

const artcleEditRef = ref()
// 添加逻辑
const onAddArticle = () => {
  artcleEditRef.value.open({})
}
//编辑逻辑
const onEditArticle = (row) => {
  console.log(row)
  artcleEditRef.value.open(row)
}
//删除逻辑
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}

//添加或者编辑成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    // 添加渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  //编辑直接渲染当前页, 在添加完成后也进行渲染
  getArticleList()
}
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单头部选择 -->
    <el-form inline>
      <el-form-item label="文章分类">
        <!-- Vue2 => v-model :value 和 @input 的简写 -->
        <!-- Vue3 => v-model :modelValue 和 @update:modelValue 的简写 -->
        <channel-select
          v-model="params.cate_id"
          style="width: 240px"
        ></channel-select>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select v-model="params.state" style="width: 240px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <!-- 显示成a标签形式 -->
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <!-- 利用作用域插槽 row 可以获取当前行的数据 => v-for 遍历 item -->
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- //分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- justify-content: flex-end" 往最后面移 框架基于flex布局的 -->

    <!-- 添加-编辑的共用抽屉 -->
    <article-edit ref="artcleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
